<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

		<script type="text/javascript" src="../jquery.js"></script>
		<script src="../ui/om-core.js"></script>
		<script src="../ui/om-mouse.js"></script>
		<script src="../ui/om-resizable.js"></script>
		<script type="text/javascript" src="../ui/om-grid.js"></script>
		<script type="text/javascript" src="../ui/om-grid-sort.js"></script>
		<script type="text/javascript" src="../ui/om-validate.js"></script>
		<script type="text/javascript" src="../ui/om-combo.js"></script>
		<script type="text/javascript" src="../ui/editor/omeditor.js"></script>
		<script src="../ui/om-messagetip.js"></script>
		<link rel="stylesheet" type="text/css"
			href="../themes/default/om-all.css" />
		<link rel="stylesheet" type="text/css"
			href="../themes/default/om-grid-sort.css" />
		<link rel="stylesheet" type="text/css"
			href="../demos/common/css/demo.css" />
		<link href="../images/skin.css" rel="stylesheet" type="text/css" />
		<script src="../ui/om-draggable.js"></script>
		<script src="../ui/om-position.js"></script>
		<script src="../ui/om-dialog.js"></script>
		<script type="text/javascript" src="../js/gettype.js"></script>
		<title>资源合作</title>

		<script type="text/javascript">

		
        	function showContent(index,type,e){
        		var data = $("#grid").omGrid("getData").rows[index];
                	var id = data.id;
		        	window.location.href = 'test.html?id=' + id;
        	}
        

        $(document).ready(function() {
        
        	$('#category').omCombo({
	                dataSource : [
		                {text:'资源1',value:'1'},
		                {text:'资源2',value:'2'},
		                {text:'资源3',value:'3'},
		                {text:'资源4',value:'4'},
		                {text:'资源5',value:'5'}
		        	],
	                emptyText:'请选择类别!',
	                filterStrategy : 'anywhere',
	                forceSelction : true,
	                lazyLoad : true
	       	});
        
        	var type = getArgs('type');
        	var search = getArgs('search');
        	
        	var url = '../nurtrition.do';
        	if(search == '1'){
        		url = url + '?' + 'search=1';
        	}
        	
        	
        
            var contentDialog = $("#content").omDialog({
                width: 800,
                height:500,
                autoOpen : false,
                modal : true,
                resizable : false
            });
        
            $('#grid').omGrid({
                height:480,
                title: '资源合作',
                dataSource : '/GetArticleServlet.do?operation=getArticleListByCategoryId',
                colModel : [ 
                            {header : '标题', name : 'title', width :300, align : 'left',sort:'clientSide'},
                             {header : '来源', name : 'resource', width : 250, align : 'left',sort:'clientSide'}, 
                             {header : '录入时间', name : 'time', width : 170, align : 'left',sort:'clientSide'},
                             {header : '浏览次数', name : 'views', width : 50, align : 'left',sort:'clientSide'}, 
                             {header : '类别', name : 'categoryName', width : 80, align : 'left',sort:'clientSide'},
	                         {header : '操作', name : 'operation', width: "autoExpand", align:'center', renderer:function(colValue, rowData, rowIndex){
                            	 var data = rowData;
                            	 return '<a href="#" onClick="showContent('+rowIndex+',1,event)">查看</a>&nbsp;';
                             }}]
            });
            var dialog = $("#dialog-modal").omDialog({
                width: 1100,
                autoOpen : false,
                modal : true,
                resizable : false,
                buttons : {
                    "提交" : function(){
		                submitDialog();
		                return false; //阻止form的默认提交动作
		            },
                    "取消" : function() {
                        $("#dialog-modal").omDialog("close");//关闭dialog
                    }
                }
            });
            
            //显示dialog并初始化里面的输入框的数据
            var showDialog = function(title,rowData){
                rowData = rowData || {};
                $("input[name='id']",dialog).val(rowData.id);
                $("input[name='title']",dialog).val(rowData.title);
                $("input[name='source']",dialog).val(rowData.source);
                $("input[name='categoryId']",dialog).val(rowData.categoryId);
                if(rowData.id != null){
                	$.post('../nurtrition.do?operation=single&id=' + rowData.id,function(tdata){
					var strs= new Array(); //定义一数组
					strs=tdata.split("%$%"); //字符分割
					if(strs.length == 6){
                		$('#editor1').omEditor('setData',strs[5]);
					}
			    	});
                }
                dialog.omDialog("option", "title", title);
                dialog.omDialog("open");//显示dialog
            };
            
            //dialog中点提交按钮时将数据提交到后台并执行相应的add或modify操作
            var submitDialog = function(){
                if (validator.form()) {
                	alert($('#editor1').omEditor('getData'));
	                var submitData={
	                    operation:isAdd?'add':'modify',
	                    id:$("input[name='id']",dialog).val(),
	                    title:$("input[name='title']",dialog).val(),
	                    material:$("input[name='material']",dialog).val(),
	                    type:$("input[name='type']",dialog).val(),
	                    content:$('#editor1').omEditor('getData')
	                };
	                if(isAdd){
			                $.post('../addNurtrition.do?operation=add',submitData,function(){
			                        $('#grid').omGrid('reload',1);//如果是添加则滚动到第一页并刷新
			                        $.omMessageTip.show({title: "操作成功", content: "添加营养学数据成功", timeout: 3000});
			           		});
	                    }else{
	                    	$.post('../addNurtrition.do?operation=modify',submitData,function(){
		                        $('#grid').omGrid('reload');//如果是修改则刷新当前页
		                        $.omMessageTip.show({title: "操作成功", content: "修改营养学数据成功", timeout: 3000});
	                        });
	                    }
	                    dialog.omDialog("close"); //关闭dialog
                }
            };
            
            var isAdd = true; //弹出窗口中是添加操作还是修改操作？
            $('#create').bind('click', function() {
                isAdd = true;
                validator.resetForm();
                showDialog('新增营养学数据');//显示dialog
            });
            
            $('#browse').bind('click', function() {
                contentDialog.omDialog("option", "title", "正文");
                contentDialog.omDialog("open");//显示dialog
            });
            
            $('#modify').bind('click', function() {
                var selections=$('#grid').omGrid('getSelections',true);
                if (selections.length == 0) {
                    alert('请至少选择一行记录');
                    return false;
                }
                isAdd = false;
                showDialog('修改营养学数据',selections[0]);//显示dialog
            });

            $('#delete').bind('click', function(e) {
                var selections=$('#grid').omGrid('getSelections',true);
                if (selections.length == 0) {
                    alert('请至少选择一行记录');
                    return false;
                }
                //将选择的记录的id传递到后台去并执行delete操作
                var toDeleteRecordID=selections[0].id;
                $.post('../addNurtrition.do',{operation:'delete',id:toDeleteRecordID},function(){
                    $('#grid').omGrid('reload');//刷新当前页数据
                    $.omMessageTip.show({title: "操作成功", content: "删除营养学数据成功", timeout: 1500});
                });
            });
            
        	//对表单进行校验
            var validator = $('#nurtritionForm').validate({
                rules : {
                    title : {required : true}
                }, 
                messages : {
                    title:{required:"标题不能为空"}
                }
            });
            
            var editor = $('.editor').omEditor({
            	filebrowserImageUploadUrl : '../omEditorImageUpload.do?type=Images'
            });
        });
    </script>
	<style media=print>
		.Noprint {
			display: none;
		}
		
		.page {
			page-break-after: always;
		}
	</style>

	</head>
	<body>

		<div style="width: 100%;" class="Noprint">
			<table width="100%" border="0" cellpadding="0" cellspacing="0">
				<tr>
					<td width="17" valign="top" background="../images/mail_leftbg.gif">
						<img src="../images/left-top-right.gif" width="17" height="29" />
					</td>
					<td valign="top" background="../images/content-bg.gif">
						<table width="100%" height="31" border="0" cellpadding="0"
							cellspacing="0" class="left_topbg" id="table2">
							<tr>
								<td height="31">
									<div class="titlebt"></div>
								</td>
							</tr>
						</table>
					</td>
					<td width="16" valign="top" background="../images/mail_rightbg.gif">
						<img src="../images/nav-right-bg.gif" width="16" height="29" />
					</td>
				</tr>
				<tr width="100%">
					<td valign="middle" background="../images/mail_leftbg.gif">
						&nbsp;
					</td>
					<td valign="top" bgcolor="#F7F8F9">
						<div id="operation" style="text-align:center;background-color:#66ccff;">
							<input id="create" type="button" value="新增" />
							<input id="modify" type="button" value="修改" />
							<input id="delete" type="button" value="删除" />
							<input id="category" name="category"/>
							<input id="search" type="button" value="查询" />
						</div>
						<div id="dialog-modal" title="新增">
							<form id="nurtritionForm">
								<input name="id" style="display: none" />
								<table>
									<tr>
										<td>
											标&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题：
										</td>
										<td>
											<input id="title" name="title" size="100" />
										</td>
									</tr>
									<tr>
										<td>
											来&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;源：
										</td>
										<td>
											<input id="source" name="source" size="100" />
										</td>
									</tr>
									<tr>
										<td>
											类&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别：
										</td>
										<td>
											<select name="categoryId">
												<option value="1">
													--国内--
												</option>
												<option value="2">
													--国外--
												</option>
											</select>
										</td>
									</tr>
									<tr>
										<td>
											正&nbsp;&nbsp;&nbsp;&nbsp;文：
										</td>
										<td width='100%'>
											<textarea name="content" id="editor1" class="editor"></textarea>
										</td>
									</tr>
								</table>
							</form>
						</div>
						<div style="padding: 0px 0px;">
							<table id="grid"></table>
						</div>
					</td>
					<td background="../images/mail_rightbg.gif">
						&nbsp;
					</td>
				</tr>
				<tr>
					<td valign="bottom" background="../images/mail_leftbg.gif">
						<img src="../images/buttom_left2.gif" width="17" height="17" />
					</td>
					<td background="../images/buttom_bgs.gif">
						<img src="../images/buttom_bgs.gif" width="17" height="17">
					</td>
					<td valign="bottom" background="../images/mail_rightbg.gif">
						<img src="../images/buttom_right2.gif" width="16" height="17" />
					</td>
				</tr>
			</table>
		</div>
		
		<div id="content" title="正文">
			<div id="bt"
				style="font-size: 28px; font-weight: bold; text-align: center; padding: 5px 0px;"></div>
			<div style="font: 12px; text-align: center; line-height: 3em;">
				录入时间：
				<span id="time"></span>&nbsp;&nbsp;&nbsp;&nbsp;浏览次数：
				<span id="number"></span>
			</div>
			<div id="zhengwen">

			</div>
		</div>
		
	</body>
</html>